package front

import (
	utils "hft/Http/Utils"
	"hft/e"
	"strconv"

	"github.com/gin-gonic/gin"

	resFront "hft/Http/Models/request/front"
	"hft/Http/Services/front"
	serveFront "hft/Http/Services/front"
)

type LouPan struct{}

func (*LouPan) GetLouPanList(c *gin.Context) {

	var paginate resFront.LouPanSearch

	c.ShouldBind(&paginate)

	city, _ := strconv.Atoi(c.Request.Header.Get("sid"))

	list, err := serveFront.LouPanService.GetLouPanList(&paginate, city)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(list, c)

}

func (*LouPan) GetLouPanInfo(c *gin.Context) {

	id, _ := strconv.Atoi(c.DefaultQuery("id", "0"))

	sid, _ := strconv.Atoi(c.Request.Header.Get("sid"))

	info, err := front.LouPanService.GetLouPanInfo(id, sid)

	if err != nil {
		utils.FailWithMessage(err.Error(), c)
		return
	}

	utils.OkWithData(info, c)

}

func (*LouPan) GetSwipeList(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("lou_id"))
	if id == 0 {
		utils.FailWithMessage(e.LouPanNotExist, c)
		return
	}
	if list, err := front.LouPanService.GetSwipeList(id); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(list, c)
	}
}
func (*LouPan) GetRoomSwipeList(c *gin.Context) {
	id, _ := strconv.Atoi(c.Query("room_id"))
	if id == 0 {
		utils.FailWithMessage(e.LouPanNotExist, c)
		return
	}
	if list, err := front.LouPanService.GetRoomSwipeList(id); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(list, c)
	}
}

func (*LouPan) AroundLou(c *gin.Context) {
	var data *resFront.AroundLou
	c.ShouldBindJSON(&data)
	if list, err := front.LouPanService.FindAroundLou(data); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(list, c)
	}
}

func (*LouPan) GetRoomList(c *gin.Context) {
	var data *resFront.RoomSearch
	c.ShouldBind(&data)
	if data.LouID == 0 {
		utils.FailWithMessage(e.LouPanHasExist, c)
		return
	}
	if list, err := front.LouPanService.GetRoomList(data); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(list, c)
	}
}

func (*LouPan) GetRoomInfo(c *gin.Context) {
	room_id, _ := strconv.Atoi(c.Query("room_id"))
	if data, err := front.LouPanService.GetRoomInfo(room_id); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(data, c)
	}
}

func (*LouPan) Guess(c *gin.Context) {
	lou_id, _ := strconv.Atoi(c.Query("loupan_id"))
	sid, _ := strconv.Atoi(c.Request.Header.Get("sid"))
	if data, err := front.LouPanService.GetGuess(lou_id, sid); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(data, c)
	}
}

func (*LouPan) GetLngLat(c *gin.Context) {
	lou_id, _ := strconv.Atoi(c.Query("loupan_id"))
	if data, err := front.LouPanService.GetLngLat(lou_id); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(data, c)
	}
}

func (*LouPan) GetPhoneCall(c *gin.Context) {
	lou_id, _ := strconv.Atoi(c.Query("loupan_id"))
	if data, err := front.LouPanService.GetPhoneCall(lou_id); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(data, c)
	}
}

func (*LouPan) GetGangAo(c *gin.Context) {
	lou_id, _ := strconv.Atoi(c.Query("loupan_id"))
	if data, err := front.LouPanService.GetGangAo(lou_id); err != nil {
		utils.FailWithMessage(err.Error(), c)
	} else {
		utils.OkWithData(data, c)
	}
}

var LouPanController = new(LouPan)
